<?php
/**
 * Created by PhpStorm.
 * User: t.young
 * Date: 2017/12/17
 * Time: 11:55
 */

namespace backend\service;


use backend\component\BackendBaseService;
use backend\component\ResultEasyUIFormat;
use backend\component\ResultFormat;
use common\component\ClassFactory;
use common\models\base\Admin;
use common\models\base\OperatorLog;
use common\models\info\Market;
use common\models\permis\Role;
use common\models\permis\RoleTask;
use common\models\permis\Task;
use common\models\permis\UserRole;
use phpDocumentor\Reflection\Types\String_;

class AdminService extends BackendBaseService
{

    /**
     * 返回后台管理员数据列表
     * @param $post
     * @return mixed
     */
    public static function adminListData($post) {
        $model = ClassFactory::getInstance(Admin::className());
        $table =  $model->generateListData($post);
        return ResultEasyUIFormat::tableFormat($table)->asJson();
    }


    public static function getMarketList($post) {
        $model = new Market();
        $table = $model->dataListTable($post);
        return ResultEasyUIFormat::tableFormat($table)->asJson();
    }


    public static function operatorListData($post) {
        $model = new OperatorLog();
        $table = $model->generateListData($post);
        return ResultEasyUIFormat::tableFormat($table)->asJson();
    }

    /**
     * 后台管理员修改自己的帐号信息及密码
     * @param $user_id
     * @param $params
     * @return string json
     */
    public static function modifyUserPassword($user_id,$params) {
        $model = ClassFactory::getInstance(Admin::className());
        $status = $model->modifyUserPwd($user_id,$params);
        if (!$status) {
            return ResultFormat::error($model->getErrorsToString());
        }
        return ResultFormat::success("保存成功");
    }

    /**
     * 保存用户对应的角色
     * @param $user_id
     * @param $role_arr
     * @return bool
     */
    public static function modifyUserToRole($user_id,$role_arr) {
        $model = ClassFactory::getInstance(UserRole::className());
        $result = $model->modifyUserToRole($user_id,$role_arr);
        return $result;
    }

    /**
     * 返回后台角色列表
     * @param $post
     * @return String_ json
     */
    public static function roleListData($post) {
        $model = ClassFactory::getInstance(Role::className());
        $table =  $model->generateListData($post);
        return ResultEasyUIFormat::tableFormat($table)->asJson();
    }

    /**
     * 获取角色树
     */
    public static function roleTree() {
        $model = ClassFactory::getInstance(Role::className());
        $roleArr = $model->findRoleRelation();
        return $roleArr;
    }

    /**
     * 获取角色，筛选出用户已有的角色
     */
    public static function roleByUid($userId) {
        $model = ClassFactory::getInstance(UserRole::className());
        $myRoleArr = $model->findUserRoles($userId);
        return $myRoleArr;
    }

    /**
     * 获取角色下的菜单
     * @param $role_ids
     */
    public static function getTaskByRoleIds($role_ids) {
        $model = ClassFactory::getInstance(Task::className());
        return $model->taskDataByRoleIds($role_ids);
    }

    /**
     * 获取菜单列表
     */
    public static function MenuListData($post) {
        $model = ClassFactory::getInstance(Task::className());
        $table = $model->menuListData($post);
        return ResultEasyUIFormat::tableFormat($table)->asJson();
    }

    /**
     * 获取菜单树
     */
    public static function menuTree(){
        $model = ClassFactory::getInstance(Task::className());
        $menuArr = $model->findMenuRelation();
        return $menuArr;
    }

    /**
     * 获取菜单 过滤角色所拥有的菜单
     */
    public static function menuListByRoleId($roleId) {
        $model = ClassFactory::getInstance(Task::className());
        $menu = $model->menuListByRoleId($roleId);
        return $menu;
    }

    /**
     * 框架初始化菜单
     * @param $role_ids string "1,2,3"
     */
    public static function loadMenu($role_ids) {
        $model = new Task();
        $menus = $model->iniLoadMenu($role_ids);
        return $menus;
    }

    /**
     * 更新角色菜单
     * @param $role_id
     * @param $task_ids
     * @return string
     */
    public static function modifyRoleTask($role_id,$task_ids) {
        $task_ids = trim($task_ids,",");
        $model = ClassFactory::getInstance(RoleTask::className());
        $status = $model->modifyRoleTask($role_id,$task_ids);
        if (!$status) {
            return ResultFormat::error($model->getErrorsToString());
        }
        return ResultFormat::success("保存成功");
    }
}